<?php
session_start();
$servername = "127.0.0.1";
$username = "web";
$password = "123456";
$dbname = "web2022_com";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//接收用户的输入
if (!empty($_REQUEST['username']) &&
    !empty($_REQUEST['pwd'])
) {
    //检查用户名密码是否正确
    $usersql = "select * from user where username='" . $_REQUEST['username'] . "' and password='" . md5($_REQUEST['pwd']) . "' limit 1";
    $result = $conn->query($usersql);
    $row = $result->fetch_assoc();
    if ($row) {
        $_SESSION['user'] = $row;
        echo json_encode(['code' => 200, 'msg' => '登录成功']);
    } else {
        echo json_encode(['code' => 401, 'msg' => '用户名或密码错误']);
    }
} else {
    echo json_encode(['code' => -1, 'msg' => 'login params error']);
}
$conn->close();
exit;
